import os.path
import time

from selenium.webdriver import ActionChains
from selenium.webdriver.chrome.webdriver import WebDriver

import setting
from base.enums.enums import EnumSize
from base.utils import window
from models.book import BookInfo


class CTFile:
    def __init__(self, browser: WebDriver):
        self.username = setting.PLATFORM.get('ctfile').get('username')
        self.password = setting.PLATFORM.get('ctfile').get('password')
        self.url = 'https://home.ctfile.com/#item-files'
        self.login_url = 'https://web.ctfile.com/p/login'
        self.browser = browser

    def get_download_url(self, book_info: BookInfo):
        self.browser.get(self.url)
        cur_url = self.browser.current_url
        if cur_url == self.login_url:
            username_xpath = '//*[@id="signinSrEmail"]'
            password_xpath = '//*[@id="signinSrPassword"]'
            login_button_xpath = '//*[@id="content"]/div[1]/form/div[4]/div[2]/div[3]/div[2]/button'
            # self.browser.find_element_by_xpath(username_xpath).send_keys(self.username)
            # self.browser.find_element_by_xpath(password_xpath).send_keys(self.password)
            self.browser.find_element_by_xpath(login_button_xpath).click()
            time.sleep(1)

        self.browser.get(self.url)
        self.browser.find_element_by_xpath('//*[@id="table_files"]/tbody/tr//a[contains(text(),"book")]').click()
        for category in book_info.get_category_list():
            try:
                self.browser.find_element_by_xpath(
                    '//*[@id="table_files"]/tbody/tr//a[contains(text(),"%s")]' % category).click()
            except Exception as e:
                self.browser.find_element_by_xpath('//*[@id="upload_btn_place"]/button[2]').click()
                self.browser.find_element_by_xpath('//*[@id="form-field-8"]').send_keys(category)
                self.browser.find_element_by_xpath('//*[@id="myForm1"]/div[2]/button[1]').click()
                self.browser.find_element_by_xpath(
                    '//*[@id="table_files"]/tbody/tr//a[contains(text(),"%s")]' % category).click()

        try:
            file = self.browser.find_element_by_xpath(
                '//*[@id="table_files"]/tbody/tr//a[contains(text(),"%s")]/../../../td[1]//input[1]' % book_info.book_name)
        except Exception as e:
            book_path = book_info.get_book_path()
            print('book_path', book_path)
            window.Windows.copy_text(book_path)
            self.browser.find_element_by_xpath('//*[@id="pickfiles"]').click()
            time.sleep(1)
            window.Windows.paste_text()
            time.sleep(1)
            window.Windows.enter()
            file_size = os.path.getsize(book_path)
            time.sleep(2 + file_size / EnumSize.MB.value)
            self.browser.refresh()
        self.browser.find_element_by_xpath(
            '//*[@id="table_files"]/tbody/tr//a[contains(text(),"%s")]/../../../td[1]//span[1]' % book_info.book_name).click()
        ac = self.browser.find_element_by_xpath(
            '//*[@id="table_files"]/tbody/tr//a[contains(text(),"%s")]/../..//i[@title="获取分享链接"]' % book_info.book_name)
        ActionChains(self.browser).move_to_element(ac).perform()
        ac.click()
        download_url = self.browser.find_element_by_xpath('//*[@id="shareLink1"]').get_property("value")
        book_info.download_url = download_url
        return download_url
